package com.tianqu.tms.www.transfer.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.www.transfer.actionform.CheckConfirmActionForm;
import com.tianqu.tms.www.transfer.controller.StatusControlInputResultController;
import com.tianqu.tms.www.transfer.viewhelper.CheckConfirmViewHelper;

public class StatusControlSearchResultAction  extends TmsAction {
	
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		CheckConfirmActionForm inputForm = (CheckConfirmActionForm) form;
		CheckConfirmViewHelper view = new CheckConfirmViewHelper();
		String serial = inputForm.getSerial();
		String status = inputForm.getStatus();
		
		view.setSerial(serial);
		view.setStatus(status);
		
		view.setRemarks(inputForm.getRemarks());
		if(!StringUtils.isNotBlank(serial)) {
			String[] param = {"流水号"};
			setErrorsToRequest(request, TmsConstants.U0005, param);
			request.setAttribute(TmsConstants.VIEWHELP, view);
			return mapping.findForward(TmsConstants.SUCCESS);
		}
		
		List<TransferAcceptedInfo> list = TransferAcceptedInfo.getInstance().getAcceptInfoBySerialAndLicense(serial,"","");
		if(list == null) {
			setErrorsToRequest(request, TmsConstants.U0039, null);
			request.setAttribute(TmsConstants.VIEWHELP, view);
			return mapping.findForward(TmsConstants.SUCCESS);
		}
		TransferAcceptedInfo accept = list.get(0);
		if(list.size() > 1) {
			setErrorsToRequest(request, TmsConstants.U1405, null);
			request.setAttribute(TmsConstants.VIEWHELP, view);
			return mapping.findForward(TmsConstants.SUCCESS);
		}
//		else if("10".equals(status) && (accept.getStatus() != 11 && accept.getStatus() != 2)) {
		else if(accept.getStatus()==0 || accept.getStatus()==1 || accept.getStatus()==16) {
			setErrorsToRequest(request, TmsConstants.U1065, null);
			request.setAttribute(TmsConstants.VIEWHELP, view);
			return mapping.findForward(TmsConstants.SUCCESS);
		}
//		else if("11".equals(status) && accept.getStatus() != 10) {
//			setErrorsToRequest(request, TmsConstants.U1066_11, null);
//			request.setAttribute(TmsConstants.VIEWHELP, view);
//			return mapping.findForward(TmsConstants.SUCCESS);
//		}else if("12".equals(status) && accept.getStatus() != 10) {
//			setErrorsToRequest(request, TmsConstants.U1066_12, null);
//			request.setAttribute(TmsConstants.VIEWHELP, view);
//			return mapping.findForward(TmsConstants.SUCCESS);
//		}else if("13".equals(status) && accept.getStatus() != 10) {
//			setErrorsToRequest(request, TmsConstants.U1066_13, null);
//			request.setAttribute(TmsConstants.VIEWHELP, view);
//			return mapping.findForward(TmsConstants.SUCCESS);
//		}
		
		if("2".equals(inputForm.getButtonType())) {//save
			if(("11".equals(status)||"13".equals(status)) && !StringUtils.isNotBlank(inputForm.getRemarks())) {
				String[] param = {"备注"};
				setErrorsToRequest(request, TmsConstants.U0005, param);
				request.setAttribute(TmsConstants.VIEWHELP, view);
				return mapping.findForward(TmsConstants.SUCCESS);
			}
			StatusControlInputResultController controller = new StatusControlInputResultController();
			controller.execute(uc);
		}
		
		view.setIsShowTable("2");
		List<TransferAcceptedInfo> acceptList = accept.getAcceptInfoBySerialAndLicense4StatusControl(serial, null);	
		view.setList(acceptList);
		
		request.setAttribute(TmsConstants.VIEWHELP,view);
		return mapping.findForward(TmsConstants.SUCCESS);
	}
}
